/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package replica;

import generalResourceLalla.Costanti;
import generalResourceLalla.wsFormattedOperation.DequeueInformation;
import generalResourceLalla.wsFormattedOperation.TaskEnvelope;
import generalResourceLalla.Utilities;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Properties;
import javax.annotation.Resource;
import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import replica.transaction.FrontEnd;
import replica.NetworkParser;
import replica.communication.MessageDispatcherReplicaServer;
import replica.communication.OperationReceiver;
import replica.transaction.Operation;
import java.util.List;
import replica.communication.JMSAdapterFrontEnd;
import replica.transaction.FrontEndKiller;


/**
 *
 * @author Utente
 */


public class ReplicaServerCreator 
{
   /* @Resource(mappedName = "jms/DueQueue")
    private static Queue dueQueue;
    @Resource(mappedName = "jms/DueQueueFactory")
    private static ConnectionFactory dueQueueFactory;*/
    
    
  public static void main(String[] args)
  {
      NetworkParser parser=null;
      ReplicaLoggerFactory.cleanServerLogFolder();
        try{
            parser=new NetworkParser(Costanti.configurationFile);
            
            List<Long> nodeIds = parser.getNodeIds();
            System.out.println("ReplicaServerCreator: list of FrontEnds:" + nodeIds.toString());
            
            if (nodeIds.size()==0)
                throw new Exception("Error: trying to start a ReplicaServer without FrontEnds");
            
            List<FrontEnd> frontEndList=new LinkedList<FrontEnd>();
            List<Long> replicaHostIds=Utilities.convert(CostantiReplica.replicaHostIds);
            
            OperationReceiver operationReceiver=new OperationReceiver(frontEndList);
            Thread operationReceiverThread = new Thread(operationReceiver);
            
            for (Long nodeId:nodeIds)
            {
                FrontEnd fe=new FrontEnd(nodeId,replicaHostIds,new JMSAdapterFrontEnd(nodeId),operationReceiver);
                frontEndList.add(fe);
                Thread frontEndThread = new Thread(fe);
                frontEndThread.start();
                FrontEndKiller killer = new FrontEndKiller(fe,frontEndThread);
                Thread killerThread = new Thread(killer);
                killerThread.start();
            }
            
            operationReceiverThread.start();
            
            
            MessageDispatcherReplicaServer replicaserver = new MessageDispatcherReplicaServer(frontEndList);

        }catch(IOException ioe)
        {
            System.err.println("Check if the attribute "
                    + "generalResourceLalla.Costanti.configurationFile"+
                    Costanti.configurationFile+" is correct");
            ioe.printStackTrace();
            System.exit(-2763);
        }catch(Exception e)
        {e.printStackTrace(); System.exit(-17653);}
    }
    
    
    public static int getReplica(LinkedList<ReplicaQueue> list, long id)
    {
       for(int i=0; i<list.size(); i++)
       {
           if(id==list.get(i).getNodeId())
               return i;
       }
       
       return 0;
    }
    
    /*
    public static FrontEnd createFrontEnd(long nodeId,List<Long> replicaHostIds) 
    {
        try{
            return new FrontEnd(nodeId,replicaHostIds,new JMSAdapterFrontEnd(nodeId));       
        }catch(Exception ex)
        {
            System.out.println(ex);
            System.exit(-83773); 
            return null;
        }
    }
    
    */

    private Message createJMSMessageForjmsDueQueue(Session session, Object messageData) throws JMSException {
        // TODO create and populate message to send
        TextMessage tm = session.createTextMessage();
        tm.setText(messageData.toString());
        return tm;
    }
    
    
    
}

